Imports Clo = Enhancements.Core.Logging
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Represents a format applicable to a minute part in a System.DateTime instance.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class DateTimeFormatMinute
        Inherits DateTimeFormat

        ''' <summary>Initializes a DateTimeFormatMinute instance with default values.</summary>
        Public Sub New()
        End Sub

        ''' <summary>Initializes a DateTimeFormatMinute instance with defined values.</summary>
        ''' <param name="_paddingUse">Whether to pad the result of the formatting by using _paddingCharacter, when applicable.</param>
        ''' <param name="_paddingCharacter">Character used for padding.</param>
        Public Sub New(ByVal _paddingUse As System.Boolean, ByVal _paddingCharacter As System.Char)
            PaddingUse = _paddingUse
            PaddingCharacter = _paddingCharacter
        End Sub

        Private __PaddingUse As System.Boolean = False
        ''' <summary>Gets and sets the value that defines whether to pad the result of the formatting by using PaddingCharacter. This will only happen when the formatting results in a 1-length sized value. The default value is false.</summary>
        Public Property PaddingUse() As System.Boolean
            Get
                Return __PaddingUse
            End Get
            Set(ByVal _value As System.Boolean)
                __PaddingUse = _value
            End Set
        End Property

        Private __PaddingCharacter As System.Char = " "c
        ''' <summary>Gets and sets the value used for padding. The default value is space.</summary>
        Public Property PaddingCharacter() As System.Char
            Get
                Return __PaddingCharacter
            End Get
            Set(ByVal _value As System.Char)
                __PaddingCharacter = _value
            End Set
        End Property

        ''' <summary>Builds a format expression according to the instance values.</summary>
        ''' <returns> </returns>
        Public Overrides Function ExpressionBuild() As System.String
            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)
            _returnValueStringBuilder.Append(___ExpressionPrefix)
            _returnValueStringBuilder.Append("minute")
            _returnValueStringBuilder.Append(___ExpressionSeparatorTitleProperties)
            _returnValueStringBuilder.Append("paddingUse")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(PaddingUse.ToString().ToLowerInvariant())
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append("paddingCharacter")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(PaddingCharacter)
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append(___ExpressionSuffix)

            Return _returnValueStringBuilder.ToString()
        End Function

        ''' <summary>Formats a supplied value according to the instance values.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <returns> </returns>
        Public Overrides Function Format(ByVal _value As Sys.DateTime) As System.String
            Dim _returnValue As System.String = Sys.String.Empty
            _returnValue = _value.Minute.ToString(Glo.CultureInfo.InvariantCulture)
            If PaddingUse And _returnValue.Length = 1 Then
                _returnValue = PaddingCharacter + _returnValue
            End If

            Return _returnValue
        End Function

        Friend Const ___RegularExpression As System.String = "^{minute:paddingUse=(true|false);paddingCharacter=(.);}$"

        ''' <summary>Tries to parse a format expression, returning an according instance in the _result parameter and True in the return value, when valid. When not, null is put in _result and False is returned.</summary>
        ''' <param name="_expression">Expression to inspect.</param>
        ''' <param name="_result">Instance returned when a valid format expression is used.</param>
        ''' <returns>Whether the operation proceeded normally.</returns>
        Public Shared Function TryParse(ByVal _expression As System.String, ByRef _result As DateTimeFormatMinute) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Try
                _expression.IsInRegularExpression("_expression", ___RegularExpression, True)
                Dim _paddingUse As System.Boolean = Sys.Boolean.Parse(Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(1).Value)
                Dim _paddingCharacter As System.Char = CType(Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(2).Value, System.Char)
                _result = New DateTimeFormatMinute(_paddingUse, _paddingCharacter)
                _returnValue = True
            Catch
                _result = Nothing
            End Try

            Return _returnValue
        End Function

    End Class
End Namespace
